package my.db.DAO;
import my.db.DTO.AuthenticationDTO;
import my.db.DTO.AuthorDTO;

import java.sql.*;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;

import org.apache.catalina.connector.Request;

import config.EstablishConnection;
public class AuthenticationDAO extends HttpServlet
{
	EstablishConnection conn=null;
	private Context cnt=null;
	private DataSource ds=null;
	private PreparedStatement pstmt=null;
	private ResultSet rs=null;
	private EstablishConnection econ=null;
	/*
	 * Constructor Initialized
	 */
	public AuthenticationDAO(){
		econ=EstablishConnection.CreateInstance();
	}
	/*
	 * Getting only user account
	 *  if true 
	 *  	return object ArticleTypeDTO
	 *  else 
	 *  	return null
	 */
	public AuthenticationDTO getAccountUser(String user){
		String query = "Select AccountUserName From Account Where AccountUserName=?";
		AuthenticationDTO dto=null;
		try{
			pstmt = conn.conection.prepareStatement(query);
			pstmt.setString(1,user);
			rs=pstmt.executeQuery();
			if(rs.next()){
				dto=new AuthenticationDTO();
				dto.setAccountName(rs.getString("AccountUserName"));
			}
			if(dto==null)System.out.println("Fairy :");
			else System.out.println("djsd");
			return dto;
		}catch(SQLException e){
			return null;
		}
	}
	/*
	 * Login to account
	 *  if true 
	 *  	return object of AuthenticationDTO
	 *  else 
	 *  	return null
	 */
	public AuthenticationDTO loginAccount(String uid, String pwd){
			String sql="Select U.UserID,U.UserName,U.UserGender,U.UserMail,U.UserPhone,A.Accountusername,A.AccountPassword,A.Position From ACCOUNT A,USERS U "
					+ "Where A.userid=U.userid "
					+ "and A.AccountUsername=? "
					+ "and A.AccountPassword=? "
					+ "and U.Status=1";
			AuthenticationDTO rDto=null;
			try{
				pstmt=conn.conection.prepareStatement(sql);		
				pstmt.setString(1, uid);
				pstmt.setString(2, pwd);
				rs=pstmt.executeQuery();
				if(rs.next()){
					AuthorDTO a=new AuthorDTO();
					rDto=new AuthenticationDTO();
					rDto.setAccountName(rs.getString("ACCOUNTUSERNAME"));
					rDto.setPosition(rs.getString("POSITION"));
					rDto.setAccountPassword(rs.getString("AccountPassword"));
					a.setAuthorID(rs.getInt("UserID"));
					a.setAuthorName(rs.getString("UserName"));
					a.setAuthorMail(rs.getString("UserMail"));
					a.setAuthorPhone(rs.getString("UserPhone"));
					a.setAuthorGender(rs.getInt("UserGender"));
					rDto.setAutDTO(a);
				}
				return rDto;
				}catch(SQLException e){	
					return null;				
		}
	}
	/*
	 * Remove session 
	 *  if true 
	 *  	return true
	 *  else 
	 *  	return false
	 */
	public boolean logOutAccount(HttpServletRequest request){
		HttpSession session=request.getSession(true);
		try{
			session.removeAttribute("user");
			return true;
		}catch(Exception e){
			return false;
		}
	}
	/*
	 * Insert New Account 
	 *  if true 
	 *  	return true
	 *  else 
	 *  	return false
	 */
	public boolean addAccount(AuthenticationDTO dto){
		String sql=" Insert Into Account Values(AccountID.NextVal,?,?,?,(Select Max(UserID)  From Users))";	
		try{
			pstmt=conn.conection.prepareStatement(sql);		
			pstmt.setString(1,dto.getAccountName() );
			pstmt.setString(2, dto.getAccountPassword());
			pstmt.setString(3, dto.getPosition());
			pstmt.executeUpdate();
			pstmt.close();
			return true;
			}catch(SQLException e){	
				return false;
			}
	}
	/*
	 * Remove  account
	 *  if true 
	 *  	return object ArticleTypeDTO
	 *  else 
	 *  	return null
	 */
	public boolean deleteAccount(int id){
		String sql="Update Users Set STATUS=1-status WHERE USERID=?";	
		try{
			pstmt=conn.conection.prepareStatement(sql);		
			pstmt.setInt(1, id);
			pstmt.executeUpdate();
			pstmt.close();
			return true;
			}catch(SQLException e){	
				return false;
			}
	}	
	/*
	 * Change password account
	 *  if true 
	 *  	return true
	 *  else 
	 *  	return false
	 */
	public boolean resetPwd(int id,String npwd){
		String query = "Update Account Set ACCOUNTPASSWORD=? Where AccountID=?";
		try{
			pstmt = conn.conection.prepareStatement(query);
			pstmt.setString(1,npwd);
			pstmt.setInt(2, id);
			pstmt.executeUpdate();
			pstmt.close();
			return true;
		}catch(SQLException e){
			return false;
		}
	}
}
